package com.bestcem.xm.member.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.uitls.EmailUtil;
import com.bestcem.xm.member.enums.MemberTypeEnum;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 校验
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2021/7/16
 */
public class CheckUtil {

    private final static String NUMBER_FORMAT_REGEX = "^[-]?[0-9]+(\\.[0-9]+){0,1}$";

    private final static String MOBILE_FORMAT_REGEX = "(1[3-9][0-9])[0-9]{8}";

    private final static String EMAIL_FORMAT_REGEX = "([a-zA-Z0-9_\\.\\-])+\\@([a-zA-Z0-9\\-])+(\\.([a-zA-Z0-9]{2,6}))+";


    public static ServiceResult<String> checkNumberFormat(String value) {
        if (!ReUtil.isMatch(NUMBER_FORMAT_REGEX, value)) {
            return ServiceResult.fail("数字类型格式错误，请检查");
        }
        return ServiceResult.success();
    }

    public static ServiceResult<String> checkMobilePhoneFormat(String value) {
        if (!ReUtil.isMatch(MOBILE_FORMAT_REGEX, value)) {
            return ServiceResult.fail("手机格式错误，请检查");
        }
        return ServiceResult.success();
    }

    public static ServiceResult<String> checkEmailFormat(String value) {
        if (!EmailUtil.validateEmail(value)) {
            return ServiceResult.fail("邮箱格式错误，请检查");
        }
        return ServiceResult.success();
    }

    public static ServiceResult<String> checkOutNoFormat(String value, Integer memberType) {
        // fix 20211015 修改邮箱校验规则
        if (memberType == MemberTypeEnum.INDIVIDUAL.getIndex()||
                memberType == MemberTypeEnum.INSTITUTION.getIndex()){
            if (value.length() > 30) {
                return ServiceResult.fail("外部参数格式错误，请检查");
            }
        }
        if (memberType == MemberTypeEnum.INTERIOR.getIndex()){
            if (value.length() > 50) {
                return ServiceResult.fail("外部参数格式错误，请检查");
            }
        }
        return ServiceResult.success();
    }

    public static ServiceResult<String> checkAppIdFormat(String value, Integer memberType) {
        if (value.length() > 128) {
            return ServiceResult.fail("appId不能超过128位");
        }
        return ServiceResult.success();
    }

    public static ServiceResult<String> checkOpenIdFormat(String value, Integer memberType) {
        if (value.length() > 128) {
            return ServiceResult.fail("appId不能超过128位");
        }
        return ServiceResult.success();
    }

    public static ServiceResult<String> checkUnionIdFormat(String value, Integer memberType) {
        if (value.length() > 28) {
            return ServiceResult.fail("appId不能超过128位");
        }
        return ServiceResult.success();
    }

    public static ServiceResult<String> checkAvatarFormat(String value, Integer memberType) {
        if (value.length() > 500) {
            return ServiceResult.fail("appId不能超过128位");
        }
        return ServiceResult.success();
    }

    public static ServiceResult<String> checkNicknameFormat(String value, Integer memberType) {
        if (value.length() > 16) {
            return ServiceResult.fail("appId不能超过128位");
        }
        return ServiceResult.success();
    }

    /**
     * 检查是否为日期
     *
     * @param value
     * @return
     */
    public static ServiceResult<String> checkDateTimeFormat(String value) {
        try {
            LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        } catch (Exception e) {
            return ServiceResult.fail("日期错误，请检查");
        }
        return ServiceResult.success();
    }

    /**
     * 校验日期格式
     */
    public static ServiceResult<String> checkDateTimeFormat(String value, String format) {
        try {
            DateUtil.parse(value, format);
        } catch (Exception e) {
            return ServiceResult.fail("日期错误，请检查");
        }
        return ServiceResult.success();
    }

    /**
     * 字符串转化时间
     */
    public static ServiceResult<String> checkVariousDateFormat(String dateTime){
        try {
            DateUtil.parse(dateTime);
        } catch (Exception e) {
            return ServiceResult.fail("日期错误，请检查");
        }
        return ServiceResult.success();
    }

    /**
     * 隐藏手机号
     *
     * @param mobile
     * @return
     */
    public static String hiddenMobile(String mobile) {
        if (StrUtil.isNotEmpty(mobile)) {
            return StrUtil.hide(mobile, 3, 7);
        }
        return "";
    }

    /**
     * 隐藏邮件
     *
     * @param email
     * @return
     */
    public static String hiddenEmail(String email) {
        if (StrUtil.isNotEmpty(email)) {
            return "****" + email.substring(email.indexOf("@"));
        }
        return "";
    }
}
